home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckPrinterSanity.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  5.0 KB  |  126 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008 Red Hat, Inc.
  6. ## Copyright (C) 2008 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import cups
  23. import gobject
  24. import os
  25. import smburi
  26. import subprocess
  27. from timedops import TimedOperation, TimedSubprocess
  28. import urllib
  29. from base import *
  30. class CheckPrinterSanity(Question):
  31.     def __init__ (self, troubleshooter):
  32.         Question.__init__ (self, troubleshooter, "Check printer sanity")
  33.         troubleshooter.new_page (gtk.Label (), self)
  34.         self.troubleshooter = troubleshooter
  35.  
  36.     def display (self):
  37.         # Collect information useful for the various checks.
  38.  
  39.         self.answers = {}
  40.  
  41.         answers = self.troubleshooter.answers
  42.         if not answers['cups_queue_listed']:
  43.             return False
  44.  
  45.         name = answers['cups_queue']
  46.  
  47.         parent = self.troubleshooter.get_window ()
  48.  
  49.         # Find out if this is a printer or a class.
  50.         try:
  51.             cups.setServer ('')
  52.             c = TimedOperation (cups.Connection, parent=parent).run ()
  53.             printers = TimedOperation (c.getPrinters, parent=parent).run ()
  54.             if printers.has_key (name):
  55.                 self.answers['is_cups_class'] = False
  56.                 queue = printers[name]
  57.                 self.answers['cups_printer_dict'] = queue
  58.             else:
  59.                 self.answers['is_cups_class'] = True
  60.                 classes = TimedOperation (c.getClasses, parent=parent).run ()
  61.                 queue = classes[name]
  62.                 self.answers['cups_class_dict'] = queue
  63.         except:
  64.             pass
  65.  
  66.         if self.answers.has_key ('cups_printer_dict'):
  67.             cups_printer_dict = self.answers['cups_printer_dict']
  68.             uri = cups_printer_dict['device-uri']
  69.             (scheme, rest) = urllib.splittype (uri)
  70.             self.answers['cups_device_uri_scheme'] = scheme
  71.             if scheme in ["ipp", "http"]:
  72.                 (hostport, rest) = urllib.splithost (rest)
  73.                 (host, port) = urllib.splitnport (hostport, defport=631)
  74.                 self.answers['remote_server_name'] = host
  75.                 self.answers['remote_server_port'] = port
  76.             elif scheme == "smb":
  77.                 u = smburi.SMBURI (uri)
  78.                 (group, host, share, user, password) = u.separate ()
  79.                 os.environ['HOST'] = host
  80.                 if group:
  81.                     os.environ['GROUP'] = group
  82.                     cmdline = 'LC_ALL=C nmblookup -W "$GROUP" "$HOST"'
  83.                 else:
  84.                     cmdline = 'LC_ALL=C nmblookup "$HOST"'
  85.                 try:
  86.                     p = TimedSubprocess (parent=parent,
  87.                                          timeout=5000,
  88.                                          args=cmdline, shell=True,
  89.                                          stdin=file("/dev/null"),
  90.                                          stdout=subprocess.PIPE,
  91.                                          stderr=subprocess.PIPE)
  92.                     result = p.run ()
  93.                     self.answers['nmblookup_output'] = result
  94.                     for line in result[0]:
  95.                         if line.startswith ("querying"):
  96.                             continue
  97.                         spc = line.find (' ')
  98.                         if spc != -1:
  99.                             # Remember the IP address.
  100.                             self.answers['remote_server_name'] = line[:spc]
  101.                             break
  102.                 except OSError:
  103.                     # Problem executing command.
  104.                     pass
  105.             elif scheme == "hp":
  106.                 os.environ['URI'] = uri
  107.                 try:
  108.                     p = TimedSubprocess (parent=parent,
  109.                                          timeout=3000,
  110.                                          args='LC_ALL=C DISPLAY= hp-info -d"$URI"',
  111.                                          shell=True,
  112.                                          stdin=file("/dev/null"),
  113.                                          stdout=subprocess.PIPE,
  114.                                          stderr=subprocess.PIPE)
  115.                     self.answers['hplip_output'] = p.run ()
  116.                 except OSError:
  117.                     # Problem executing command.
  118.                     pass
  119.  
  120.             r = cups_printer_dict['printer-type'] & cups.CUPS_PRINTER_REMOTE
  121.             self.answers['cups_printer_remote'] = (r != 0)
  122.         return False
  123.  
  124.     def collect_answer (self):
  125.         return self.answers
  126.